#include "../include/source.hpp"


soulc::Source::Source()
{}

soulc::Source::Source(std::string fileName)
{
	_sourceFileName = fileName;
	_sourceFile.open(_sourceFileName, std::ifstream::in);

	readSource();
}


std::string soulc::Source::getFileName()
{ return _sourceFileName; }

void soulc::Source::setFilename(std::string fileName)
{
	if (!_sourceFileName.empty()) {
		raiseError(COMPILER_SOURCE_FILENAME_ALREADY_SET_ERR);
	}

	_sourceFileName = fileName;
	_sourceFile.open(_sourceFileName, std::ifstream::in);
}


void soulc::Source::readSource()
{
	if (!_source.empty()) {
		raiseError(COMPILER_SOURCES_ALREADY_READ_ERR);
	}


	if (!_sourceFile.is_open()) {
		raiseError(COMPILER_UNABLE_TO_OPEN_SOURCE_FILE_ERR, _sourceFileName);
	}

	char curChar = _sourceFile.get();

	while (_sourceFile.good()) {
		_source += curChar;
		curChar = _sourceFile.get();
	}

	_sourceFile.close();
}

std::string soulc::Source::getSource()
{ return _source; }


bool soulc::Source::isPackage()
{ return _isPackage; }

void soulc::Source::makeItAPackage(std::string packageName)
{
	_isPackage = true;
	_packageName = packageName;
}
